//
//  GGAlertController.swift
//  PanModalDemo
//
//  Created by Mr.Gao on 2021/2/2.
//  Copyright © 2021 Detail. All rights reserved.
//

import UIKit

class GGAlertController: UIAlertController {
    
    /// title 颜色
    var titleColor = UIColor.black
    /// title 字体大小
    var titleFont = UIFont.systemFont(ofSize: 17)
    
    /// message 颜色
    var messageColor = UIColor.black
    /// message 字体大小
    var messageFont = UIFont.systemFont(ofSize: 15)
    
    /// action 默认字体颜色
    var actionDefaultColor = UIColor.black
    /// action cancel字体颜色
    var actionCancelColor = UIColor.black
    /// action destructive字体颜色
    var actionDestructiveColor = UIColor.black
    
    override func viewDidLoad() {
        super.viewDidLoad()
        var count:UInt32 = 0
        let ivars = class_copyIvarList(UIAlertController.self, &count)
        for i in 0..<Int(count) {
            if let ivar = ivars?[i], let ivarName = String(cString: ivar_getName(ivar)!, encoding: .utf8) {
                let defaultColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
                //默认标题颜色
                if (ivarName == "_attributedTitle") && title != nil {
                    let attr = NSMutableAttributedString(string: title ?? "", attributes: [
                        NSAttributedString.Key.foregroundColor: defaultColor,
                        NSAttributedString.Key.font: titleFont
                    ])
                    setValue(attr, forKey: "attributedTitle")
                }
                //默认描述颜色
                if (ivarName == "_attributedMessage") && message != nil {
                    let attr = NSMutableAttributedString(string: message!, attributes: [
                        NSAttributedString.Key.foregroundColor: defaultColor,
                        NSAttributedString.Key.font: messageFont
                    ])
                    setValue(attr, forKey: "attributedMessage")
                }
                //自定义标题颜色
                if (ivarName == "_attributedTitle") && title != nil{
                    let attr = NSMutableAttributedString(string: title ?? "", attributes: [
                        NSAttributedString.Key.foregroundColor: titleColor,
                        NSAttributedString.Key.font: titleFont
                    ])
                    setValue(attr, forKey: "attributedTitle")
                }
                //自定义描述颜色
                if (ivarName == "_attributedMessage") && message != nil {
                    let attr = NSMutableAttributedString(string: message!, attributes: [
                        NSAttributedString.Key.foregroundColor: messageColor,
                        NSAttributedString.Key.font: messageFont
                    ])
                    setValue(attr, forKey: "attributedMessage")
                }
            }
        }
        
        for action in actions where action is GGAlertAction {
            if action.style == .cancel {
                (action as! GGAlertAction).textColor = actionCancelColor
            } else if action.style == .destructive {
                (action as! GGAlertAction).textColor = actionDestructiveColor
            } else {
                (action as! GGAlertAction).textColor = actionDefaultColor
            }
        }
    }
    
}

class GGAlertAction: UIAlertAction {
    
    var textColor = UIColor.black {
        didSet{
            configureTextColor(textColor)
        }
    }
    
    //MARK: - 重写UIAlertAction的颜色
    func configureTextColor(_ textColor: UIColor) {
        var count:UInt32 = 0
        let ivars = class_copyIvarList(UIAlertAction.self, &count)
        for i in 0..<Int(count) {
            let ivar = ivars?[i]
            var ivarName: String? = nil
            if let ivar = ivar, let tempName = ivar_getName(ivar) {
                ivarName = String(cString: tempName)
            }
            if ivarName == "_titleTextColor" {
                setValue(textColor, forKey: "titleTextColor")
            }
        }
    }
}
